package test;

public class TicketRunnable implements Runnable {

    static private int ticket = 20;
    static final Object obj = new Object();
    @Override
    public void run() {
        while(true) {
            synchronized (obj) { //互斥锁对象(可以是任意的java对象，但是要保证对象唯一)
                if(ticket < 0) {
                    break;
                }
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //获取线程的名称
                System.out.println(Thread.currentThread().getName()+"卖出了:"+ ticket-- +"号票");
            }
        }
    }
}
